package com.fr.data.pool;

import itsm.isperp.framework.core.context.ContextHolder;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.sql.DataSource;

import com.fr.general.Inter;
import com.fr.json.JSONArray;
import com.fr.json.JSONException;
import com.fr.json.JSONObject;
import com.fr.third.org.apache.commons.dbcp.BasicDataSource;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-3-2
 * @since 1.0
 * 
 */
public class MemoryConnection {

	private static final Map M_POOLTABLE = new Hashtable();

	public static Connection create(String paramString1, String paramString2,
			String paramString3, String paramString4,
			DBCPConnectionPoolAttr paramDBCPConnectionPoolAttr)
			throws SQLException {
		return getDataSource(paramString1, paramString2, paramString3,
				paramString4, paramDBCPConnectionPoolAttr).getConnection();
	}

	private static DataSource getDataSource(String paramString1,
			String paramString2, String paramString3, String paramString4,
			DBCPConnectionPoolAttr paramDBCPConnectionPoolAttr) {
		return (DataSource) ContextHolder.getSpringBean("dataSource");
	}

	public static JSONArray getJSONConnectionInfo() throws JSONException {
		JSONArray localJSONArray = new JSONArray();
		int i = M_POOLTABLE.size();
		String[] arrayOfString = new String[i];
		Set localSet = M_POOLTABLE.keySet();
		int j = 0;
		Iterator localIterator = localSet.iterator();
		while (localIterator.hasNext()) {
			String str = (String) localIterator.next();
			BasicDataSource localBasicDataSource = (BasicDataSource) M_POOLTABLE
					.get(str);
			JSONObject localJSONObject = new JSONObject();
			localJSONObject.put(
					Inter.getLocText("Data_Connection_Driver_Name"),
					localBasicDataSource.getDriverClassName());
			localJSONObject.put(Inter.getLocText("DBCP_MAX_ACTIVE"),
					localBasicDataSource.getMaxActive());
			localJSONObject.put(Inter.getLocText("DBCP_MAX_IDLE"),
					localBasicDataSource.getMaxIdle());
			localJSONObject.put(Inter.getLocText("Active_Connection_Size"),
					localBasicDataSource.getNumActive());
			localJSONObject.put(Inter.getLocText("Idle_Connection_Size"),
					localBasicDataSource.getNumIdle());
			localJSONArray.put(localJSONObject);
			j++;
		}
		return localJSONArray;
	}

	public static String[] getConnectionInfo() {
		return new String[0];
	}

	public static Map getConnectionMap() {
		return M_POOLTABLE;
	}
}
